home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / cexpert.zip / MCH7.LST < prev    next >
File List  |  1990-09-15  |  47KB  |  1,873 lines

  1. Listing 7-1 The Inventory Control System Class Structure in C++
  2.  
  3. /*****************************************************************************
  4.        This header file defines the knowledge representation data structure
  5.     in inventory control system in C++.
  6.  
  7.     Inventory Control System Class structure :
  8.         
  9.                              Basic_Class
  10.                   |
  11.       ---------------------------------------------------          
  12.       |              |                 |              |
  13.     Location_Class   Inventory_Class    Customer_Class   Sales_Records_Class
  14.       |              |                   |              |
  15.       ----------      -------------    ------------     sf_sales_record_class
  16.       |        |      |           |     |          |
  17.   Warehouse  Dealer WH_Inv      DL_Inv ST_Cus     HY_Cus
  18.                       |           |    
  19.             SF_Inv     --------
  20.                    |      |
  21.                  ST_Inv  HY_Inv
  22.                      
  23.                      
  24.            
  25.  
  26. *****************************************************************************/
  27. /*
  28. **  icsclass.h
  29. **
  30. **  Define each class.
  31. **  
  32. **  Date:      9/6/88
  33. */
  34.  
  35.  
  36. /*---------------------------------------------------define class-----------*/
  37.  
  38.  
  39. class basic_class {
  40.     friend class   location;                     
  41.     friend class   inv;
  42.     friend class   cust;
  43.     friend class   sales_records;           
  44. };
  45.  
  46.  
  47. /*---------------------------------------------------cust class-------*/
  48.  
  49. class cust : public basic_class {
  50.     char_slot       *dealer;
  51.     char_slot       *name;
  52.     char_slot       *part;
  53.     int_slot        *number;
  54.     char_slot       *order_date;è    char_slot       *time;
  55.     friend class st_cust;
  56.     friend class hayward_cust;
  57. public:
  58.     cust();
  59.     ~cust() { delete dealer; delete name; delete part;
  60.               delete order_date; delete number; delete time;
  61.         }   /*destructor*/
  62.     void  set_slotval();                /*set the slot value*/
  63.     Cust* get_slotval();                    /*get the slot value*/
  64.     void  prn_value();
  65. };
  66.  
  67. /*------------------------------------------------st_cust class---*/
  68. /*Inheritence from class cust
  69. */
  70.  
  71. class st_cust : public cust {};
  72.  
  73.  
  74. /*---------------------------------------------------inv class-------*/
  75.  
  76. class inv : public basic_class {
  77.     char_slot    *name;                     /*part name*/
  78.     int_slot     *cil;                       /*current inv level*/
  79.     int_slot     *oil;                       /*optimal inv level*/
  80.     friend class dealer_inv;
  81.     friend class warehs_inv;
  82. public:
  83.     inv();
  84.     ~inv() { delete name; delete cil; delete oil;}
  85.     void set_slotval();
  86.     Inv* get_slotval();
  87.     void prn_value();
  88. };
  89.  
  90.  
  91. /*--------------------------------------------dealer_inv class-----*/
  92. /*Inherited from class inv
  93. */
  94. class dealer_inv : public inv {};
  95.  
  96. /*------------------------------------------------st_inv class--------*/
  97. /*Inherited from class dealer_inv
  98. */
  99. class st_inv : public dealer_inv {};
  100.  
  101.  
  102. /*--------------------------------------------warehs_inv class-----*/
  103. /*Inherited from class inv
  104. */
  105.  
  106. class warehs_inv : public inv { 
  107.     double_slot   *cost;
  108.     double_slot   *discount;è    int_slot      *min_order;
  109.     char_slot     *order_time;
  110. public:
  111.     warehs_inv();
  112.     ~warehs_inv() { delete order_time; delete cost;
  113.                  delete min_order; delete discount;}
  114.     void set_slotval1();
  115.     Ware_Inv* get_slotval1();
  116.     void prn_value1();
  117. };
  118.  
  119.  
  120. /*--------------------------------------------sf_inv class-----*/
  121. /*Inheritence from class warehs_inv 
  122. */
  123.  
  124. class sf_inv : public warehs_inv {};
  125.  
  126.  
  127. /*-------------------------------------------------location class-----------*/
  128.  
  129. class location : public basic_class {
  130.     char_slot           *address;              /*address of the location*/
  131.     char_slot           *phone;                /*phone number of the loc*/
  132.     class  invent : public inv {} *invntory;   /*inventory subclass*/
  133.     friend class dealer;
  134.     friend class warehs;
  135. public:    
  136.     location();
  137.     ~location() {  delete address; delete phone;}
  138.     void set_slotval();                     /*set the slot value*/
  139.     Loc* get_slotval1();                    /*get the address, phone*/
  140.     Inv* get_slotval2();           /*get the subclass inventory value*/
  141.     void prn_value();
  142. };
  143.  
  144.  
  145. /*-------------------------------------------------dealer class-----------*/
  146. /*Inherited from class location
  147. */
  148.  
  149. class dealer : public location {  
  150.     class  custmers : public cust {} *custm;
  151. public:
  152.     dealer() { custm = new custmers;}
  153.     ~dealer() { delete custm;}
  154.     void set_cust_slotval();
  155.         Cust* get_cust_slotval();
  156.     void prn_cust_slotval();
  157. };
  158.  
  159.  
  160.  
  161. /*--------------------------------------------sales_records class-------*/
  162. class sales_records : public  basic_class {
  163.     int_slot            *sale_number;
  164.     char_slot           *date;
  165.     char_slot        *part;
  166.     int_slot        *number_sold;
  167.     double_slot         *cost;
  168.     double_slot        *total_sale;
  169.     char_slot           *sold_to;
  170.     friend class sf_sales_records;
  171. public:
  172.     sales_records();
  173.         ~sales_records() { delete date; delete part; 
  174.                        delete sold_to; delete sale_number;
  175.                    delete number_sold; delete cost;
  176.                delete total_sale;}
  177.     void set_slotval();
  178.     Sales_Rec* get_slotval();
  179.     void prn_value();
  180. };
  181.  
  182.  
  183. /*--------------------------------------------sf_sales_records class--*/
  184. /*Inherited from class sales_records 
  185. */
  186.  
  187. class sf_sales_records : public sales_records {};
  188.  
  189.  
  190. /*-------------------------------------------------warehs class---------*/
  191. /*Inherited from class location
  192. */
  193.  
  194. class warehs : public location {  
  195.     class  sales_recs : public sales_records {} *sales_rec;
  196. public:
  197.     warehs() {sales_rec = new sales_recs;}
  198.     ~warehs() { delete sales_rec;}
  199.     void set_sales_recs_slotval();
  200.         Sales_Rec* get_sales_recs_slotval();
  201. };
  202.  
  203.  
  204.  
  205. /*
  206. ****************************************************************
  207. **
  208. **  ics.h
  209. **
  210. **  Define the overloaded functions which are used in building up KB.
  211. **  Date:    9/6/88
  212. **
  213. */
  214.  
  215.  
  216. #define    FAIL          0
  217. #define    SUCCEED       1
  218.  
  219.  
  220. struct slot_node {                         /*slot node used in slot list*/
  221.     char*          name;
  222.     slot_node*      next;
  223. };
  224.  
  225. struct cls_slot {
  226.     char*             name;
  227.     slot_node*        sllist;
  228. };
  229.  
  230.  
  231. /*-----------------------------------------------------For ICS -------------*/
  232. /*
  233. **  Define each class node and entity node structure for the leaves of class
  234. **  tree. Because C++ is compiler language instead of intepreter language.
  235. **  So it is impossible to dynamic define the data structure in the run time.
  236. */
  237.  
  238. /*
  239. ** st_cust class
  240. */
  241. struct stcust_etynd {               /*st_cust class entity node*/
  242.     char*                 name;
  243.     st_cust*                 entity;
  244.     stcust_etynd*            next;
  245. };
  246.  
  247. struct stcust_class {                 /*st_cust class node*/
  248.     char*                 name;
  249.     slot_node*                 slist;          
  250.     stcust_etynd*             elist;
  251. };
  252.  
  253. /*
  254. ** st inv  class
  255. */
  256. struct stinv_etynd {             /*st_inv class entity node*/
  257.     char*                 name;
  258.     st_inv*                  entity;
  259.     stinv_etynd*             next;
  260. };
  261.  
  262. struct stinv_class {              /*st_inv class node*/
  263.     char*                  name;
  264.     slot_node*                  slist;          
  265.     stinv_etynd*              elist;
  266. };
  267.  
  268. /*
  269. ** san francisco inv  class
  270. */
  271. struct sfinv_etynd {             /*sf_inv class entity node*/
  272.     char*                 name;
  273.     sf_inv*                  entity;
  274.     sfinv_etynd*             next;
  275. };
  276.  
  277. struct sfinv_class {              /*sf_inv class node*/
  278.     char*                  name;
  279.     slot_node*                  slist;          
  280.     sfinv_etynd*              elist;
  281. };
  282.  
  283. /*
  284. **  warehouse class
  285. */
  286. struct warehs_etynd {     
  287.     char*                 name;
  288.     warehs*                  entity;
  289.     warehs_etynd*            next;
  290. };
  291.  
  292. struct warehs_class {
  293.     char*                  name;
  294.     slot_node*          slist;
  295.     warehs_etynd*          elist;
  296. };
  297.  
  298. /*
  299. **  dealer class
  300. */
  301. struct dealer_etynd {     
  302.     char*                 name;
  303.     dealer*                  entity;
  304.     dealer_etynd*            next;
  305. };
  306.  
  307. struct dealer_class {
  308.     char*                  name;
  309.     slot_node*          slist;
  310.     dealer_etynd*          elist;
  311. };
  312.  
  313. /*
  314. **  sf_sales_records class
  315. */
  316. struct sfsalrec_etynd {     
  317.     char*                 name;
  318.     sf_sales_records*        entity;
  319.     sfsalrec_etynd*          next;
  320. };
  321.  
  322. struct sfsalrec_class {
  323.     char*                  name;
  324.     slot_node*          slist;
  325.     sfsalrec_etynd*          elist;
  326. };
  327.  
  328. /*---------------------------------------------Overload define-----------*/
  329. /*
  330. ** The best way to generalize the routines.
  331. */
  332. overload addslot_clsnd;
  333. void addslot_clsnd(cls_slot*,stcust_class*);     /*for st_cust class*/
  334. void addslot_clsnd(cls_slot*,stinv_class*);      /*for st_inv class*/
  335. void addslot_clsnd(cls_slot*,sfinv_class*);      /*for sf_inv class*/
  336. void addslot_clsnd(cls_slot*,warehs_class*);     /*for warehs class*/
  337. void addslot_clsnd(cls_slot*,dealer_class*);     /*for dealer class*/
  338. void addslot_clsnd(cls_slot*,sfsalrec_class*);   /*for sf_sales_rec class*/
  339.  
  340. overload addety_clsnd;
  341. void addety_clsnd(char*,st_cust*,stcust_class*); /*for st_cust class*/
  342. void addety_clsnd(char*,st_inv*,stinv_class*);   /*for st_inv class*/
  343. void addety_clsnd(char*,sf_inv*,sfinv_class*);   /*for sf_inv class*/
  344. void addety_clsnd(char*,warehs*,warehs_class*);  /*for warehs class*/
  345. void addety_clsnd(char*,dealer*,dealer_class*);  /*for dealer class*/
  346. void addety_clsnd(char*,sf_sales_records*,sfsalrec_class*);  /*for sf_sales_rec class*/
  347.  
  348. overload print_entity_name;
  349. void print_entity_name(stcust_etynd*);     /*for st_cust class*/
  350. void print_entity_name(stinv_etynd*);      /*for st_inv class*/
  351. void print_entity_name(sfinv_etynd*);      /*for sf_inv class*/
  352. void print_entity_name(warehs_etynd*);     /*for warehs class*/
  353. void print_entity_name(dealer_etynd*);      /*for dealer class*/
  354. void print_entity_name(sfsalrec_etynd*);      /*for sf_sales_rec class*/
  355.  
  356. overload print_clsnode;
  357. void print_clsnode(stcust_class*);        /*for st_cust class*/
  358. void print_clsnode(stinv_class*);         /*for st_inv class*/ 
  359. void print_clsnode(sfinv_class*);         /*for sf_inv class*/
  360. void print_clsnode(warehs_class*);        /*for warehs class*/
  361. void print_clsnode(dealer_class*);        /*for dealer class*/
  362. void print_clsnode(sfsalrec_class*);      /*for sf sale rec class*/
  363.  
  364. overload find_entity_node;
  365. st_cust* find_entity_node(char*,stcust_class*);  /*for st_cust class*/
  366. st_inv*  find_entity_node(char*,stinv_class*);   /*for st_inv class*/
  367. sf_inv*  find_entity_node(char*,sfinv_class*);   /*for sf_inv class*/
  368. warehs*  find_entity_node(char*,warehs_class*);  /*for warehs class*/
  369. dealer*  find_entity_node(char*,dealer_class*);  /*for dealer class*/
  370. sf_sales_records*  find_entity_node(char*,sfsalrec_class*);  /*for sf sale class*/
  371.  
  372. overload find_all_entities;
  373. stcust_etynd* find_all_entities(stcust_class*);  /*for st_cust class*/
  374. stinv_etynd* find_all_entities(stinv_class*);    /*for st_inv class*/
  375. sfinv_etynd* find_all_entities(sfinv_class*);    /*for sf_inv class*/
  376. warehs_etynd* find_all_entities(warehs_class*);  /*for warehs class*/
  377. dealer_etynd* find_all_entities(dealer_class*);  /*for dealer class*/
  378. sfsalrec_etynd* find_all_entities(sfsalrec_class*);  /*for sf sal rec class*/
  379.  
  380. overload find_e_with_slot;
  381. stcust_etynd* find_e_with_slot(char*,char*, stcust_class*);  /*for st_cust class*/
  382. stinv_etynd* find_e_with_slot(char*,char*, stinv_class*);  /*for st_inv class*/
  383. sfinv_etynd* find_e_with_slot(char*,char*,sfinv_class*);  /*for sf_inv class*/
  384. warehs_etynd* find_e_with_slot(char*,char*, warehs_class*);  /*for warehs_inv class*/
  385. dealer_etynd* find_e_with_slot(char*,char*,dealer_class*);  /*for dealer_inv class*/
  386. sfsalrec_etynd* find_e_with_slot(char*,char*,sfsalrec_class*);  /*for sf sales records class*/
  387.  
  388.  
  389. /*
  390. ***********************************************************
  391. **
  392. **  slotdef.h
  393. **
  394. **  Define the slot structure
  395. **  Date:    9/6/88
  396. */
  397.  
  398. struct char_slot {                         /*the slot value is char*/
  399.     char*          slot_name;
  400.     char*          slot_value;
  401. };
  402.  
  403. struct double_slot {                       /*the slot value is double*/
  404.     char*          slot_name;
  405.     double          slot_value;
  406. };
  407.  
  408. struct int_slot {                          /*the slot value is int*/ 
  409.     char*          slot_name;
  410.     int          slot_value;
  411. };
  412.  
  413. #define    SLOTNAME      30
  414. #define    SLOTVALUE     30
  415. #define    SLOT_END      "END"
  416.  
  417. /*
  418. ******************************************************
  419. **
  420. **   parse.h
  421. **   
  422. **   Define the structure used in parsing the query.
  423. **   Date:     9/6/88
  424. **
  425. */
  426.  
  427.  
  428. #define   CASE1   1    /*C and E are instantiated without slot*/
  429. #define   CASE2   2    /*C is instantiated but E isn't without slot*/
  430. #define   CASE3   3    /*C and E are instantiated with slot var*/
  431. #define   CASE4   4    /*C is instantiated but E isn't with slot var*/
  432. #define   CASE5   5    /*C and E are instantiated with slot constant*/
  433. #define   CASE6   6    /*C is instantiated but E isn't with slot constant*/
  434. #define   CASE7   7    /*entity is instantiated and class isn't*/
  435. #define   ZERO    0
  436. #define   ONE     1
  437. #define   TWO     2
  438. #define   THREE   3
  439. #define   FOUR    4
  440. #define   FIVE    5
  441. #define   SIX     6
  442. #define   SEVEN   7
  443. #define   EIGHT   8
  444.  
  445.  
  446. /*
  447. **
  448. *********************************************************************
  449. **     tempstru.h
  450. **
  451. **     This header file defines some structures for inputing and outputing
  452. **     the class slot data.
  453. **        
  454. **     Include:    Location,Inventory,Customer
  455. **     Date:       7/22/88           
  456. */
  457.  
  458. struct Loc {                                /*location*/ 
  459.     char_slot       *address;              /*address of the location*/
  460.     char_slot       *phone;                /*phone number of the loc*/
  461. };
  462.  
  463.  
  464. struct Inv {                                /*inventory*/
  465.     char_slot         *name;             /*part name*/
  466.     int_slot          *cil;              /*current inventory level*/
  467.     int_slot          *oil;              /*optimal inventory level*/
  468. };
  469.  
  470.  
  471. struct Cust {                                /*customer*/
  472.     char_slot            *dealer;
  473.     char_slot            *name;
  474.     char_slot            *part;
  475.     int_slot             *number;
  476.     char_slot            *order_date;
  477.     char_slot            *time;
  478. };
  479.  
  480. struct Sales_Rec {                    /*sales records*/
  481.     int_slot        *sale_number;
  482.     char_slot       *date;
  483.     char_slot       *part;
  484.     int_slot     *number_sold;
  485.     double_slot    *cost;
  486.     double_slot    *total_sale;
  487.     char_slot    *sold_to;
  488. };
  489.  
  490. struct Ware_Inv {                      /*warehouse inventory*/
  491.     double_slot        *cost;
  492.     double_slot         *discount;
  493.     int_slot        *min_order;
  494.     char_slot        *order_time;
  495. };
  496.  
  497.  
  498.  
  499. Listing 7-2  The ICS Class Constructors and Member Functions
  500.  
  501. /* 
  502. **  icskb0.cpp
  503. **  Define the member functions of each class.
  504. **
  505. **  Date:    9/6/88
  506. */
  507.  
  508. #include <stream.h>
  509. #include <string.h>
  510. #include <slotdef.h>
  511. #include <tempstru.h>
  512. #include <icsclass.h>
  513.  
  514. /*-------------------------------------------------------cust_class----*/
  515.  
  516. cust::cust() 
  517. {
  518.      
  519.      dealer->slot_name = new char[SLOTNAME];
  520.      dealer->slot_name = "dealer";
  521.      dealer->slot_value = new char[SLOTVALUE];
  522.      *dealer->slot_value = NULL;
  523.       name->slot_name = new char[SLOTNAME];
  524.       name->slot_name = "name";
  525.       name->slot_value = new char[SLOTVALUE];
  526.       *name->slot_value = NULL;
  527.      part->slot_name = new char[SLOTNAME];
  528.      part->slot_name = "part";
  529.      part->slot_value = new char[SLOTVALUE];
  530.      *part->slot_value = NULL;
  531.      number->slot_name = new char[SLOTNAME];
  532.      number->slot_name = "number";
  533.      number->slot_value = 0;
  534.      order_date->slot_name = new char[SLOTNAME];
  535.      order_date->slot_name = "order_date";
  536.      order_date->slot_value = new char[SLOTVALUE];
  537.      *order_date->slot_value = NULL;
  538.      time->slot_name = new char[SLOTNAME];
  539.      time->slot_name = "time";
  540.      time->slot_value = new char[SLOTVALUE];
  541.      *time->slot_value = NULL;
  542. }
  543.  
  544. Cust* cust::get_slotval()                /*get the slot value*/
  545. {
  546.     Cust* temp = new Cust;
  547.  
  548.     temp->dealer->slot_value = dealer->slot_value;
  549.     temp->dealer->slot_name = dealer->slot_name;
  550.     temp->name->slot_value = name->slot_value;
  551.     temp->name->slot_name = name->slot_name;
  552.     temp->part->slot_value = part->slot_value;
  553.     temp->part->slot_name = part->slot_name;
  554.     temp->number->slot_value = number->slot_value;
  555.     temp->number->slot_name = number->slot_name;
  556.     temp->order_date->slot_value = order_date->slot_value;
  557.     temp->order_date->slot_name = order_date->slot_name;
  558.     temp->time->slot_value = time->slot_value;
  559.     temp->time->slot_name = time->slot_name;
  560.     return temp;
  561. }
  562.  
  563.  
  564. void cust::set_slotval()                /*set the slot value*/
  565. {
  566.  
  567.  
  568.     cout << "\n" << dealer->slot_name << ":  ";
  569.     cin >> dealer->slot_value;
  570.         cout << name->slot_name << ":  ";
  571.     cin >> name->slot_value;
  572.         cout << part->slot_name << ":  ";
  573.     cin >> part->slot_value;
  574.         cout << number->slot_name << ":  ";
  575.     cin >> number->slot_value;
  576.         cout << order_date->slot_name << ":  ";
  577.     cin >> order_date->slot_value;
  578.         cout << time->slot_name << ":  ";
  579.     cin >> time->slot_value;
  580. }
  581.  
  582.  
  583. void cust::prn_value()
  584. {
  585.     cout << "\n" << dealer->slot_name << ": " << dealer->slot_value;
  586.     cout << "  " << name->slot_name << ": "  << name->slot_value;
  587.     cout << "  " << part->slot_name <<  ": " << part->slot_value;
  588.     cout << "  " << number->slot_name <<  ": " << number->slot_value;
  589.     cout << "  " << order_date->slot_name << ": " << order_date->slot_value;
  590.     cout << "  " << time->slot_name << ": " << time->slot_value;
  591. }
  592.  
  593.  
  594. /*-------------------------------------------------------inv_class----*/
  595.  
  596.  
  597. inv::inv()
  598. {
  599.      cil->slot_name = new char[SLOTNAME];
  600.      cil->slot_name = "cil";
  601.      cil->slot_value = oil->slot_value = 0;
  602.      oil->slot_name = new char[SLOTNAME];
  603.      oil->slot_name = "oil";
  604.      name->slot_name = new char[SLOTNAME];
  605.      name->slot_name = "name";
  606.      name->slot_value = new char[SLOTVALUE];
  607.      *name->slot_value = NULL;
  608. è}
  609.  
  610.  
  611. void inv::set_slotval()
  612. {
  613.     cout << "\n" << name->slot_name << ":  ";
  614.     cin >> name->slot_value;
  615.     cout << cil->slot_name << ":  ";
  616.     cin >> cil->slot_value;
  617.     cout << oil->slot_name << ":  ";
  618.     cin >> oil->slot_value;
  619. }
  620.  
  621.  
  622. Inv* inv::get_slotval()
  623. {
  624.     Inv* temp = new Inv;
  625.  
  626.     temp->name->slot_name = name->slot_name;
  627.     temp->name->slot_value = name->slot_value;
  628.     temp->cil->slot_name = cil->slot_name;
  629.     temp->cil->slot_value = cil->slot_value;
  630.     temp->oil->slot_name = oil->slot_name;
  631.     temp->oil->slot_value = oil->slot_value;
  632.     return temp;
  633. }
  634.  
  635. void inv::prn_value()
  636. {
  637.     cout << "\n" << name->slot_name << ":  " << name->slot_value;
  638.     cout << "  " << cil->slot_name << ":  " << cil->slot_value;
  639.     cout << "  " << oil->slot_name << ":  " << oil->slot_value;
  640. }
  641.  
  642.  
  643. /*--------------------------------------------warehs_inv class-----*/
  644.  
  645. warehs_inv::warehs_inv()
  646. {
  647.      cost->slot_name = new char[SLOTNAME];
  648.      cost->slot_name = "cost";
  649.      cost->slot_value = 0.0;
  650.      discount->slot_name = new char[SLOTNAME];
  651.      discount->slot_name = "discount";
  652.      discount->slot_value = 0.0;
  653.      min_order->slot_name = new char[SLOTNAME];
  654.      min_order->slot_name = "min_order";
  655.      min_order->slot_value = 0;
  656.          order_time->slot_name = new char[SLOTNAME];
  657.          order_time->slot_name = "order_time";
  658.          order_time->slot_value = new char[SLOTVALUE];
  659.          *order_time->slot_value = NULL;
  660.  
  661. }
  662. è
  663.  
  664. void warehs_inv::set_slotval1()
  665. {
  666.     cout << "\n" << cost->slot_name << ":  ";
  667.     cin >> cost->slot_value;
  668.     cout << discount->slot_name << ":  ";
  669.     cin >> discount->slot_value;
  670.     cout << min_order->slot_name << ":  ";
  671.     cin >> min_order->slot_value;
  672.     cout << order_time->slot_name << ":  ";
  673.     cin >> order_time->slot_value;
  674. }
  675.  
  676. Ware_Inv* warehs_inv::get_slotval1()
  677. {
  678.     Ware_Inv* temp = new Ware_Inv;
  679.  
  680.     temp->cost->slot_name = cost->slot_name;
  681.     temp->cost->slot_value = cost->slot_value;
  682.     temp->discount->slot_name = discount->slot_name;
  683.     temp->discount->slot_value = discount->slot_value;
  684.     temp->min_order->slot_name = min_order->slot_name;
  685.     temp->min_order->slot_value = min_order->slot_value;
  686.     temp->order_time->slot_name = order_time->slot_name;
  687.     temp->order_time->slot_value = order_time->slot_value;
  688.     return temp;
  689. }
  690.  
  691. void warehs_inv::prn_value1()
  692. {
  693.     cout << "\n" << cost->slot_name << ":  "<< cost->slot_value;
  694.     cout << "  " << discount->slot_name << ":  " << discount->slot_value;
  695.     cout << "  " << min_order->slot_name << ":  " << min_order->slot_value;
  696.     cout << "  " << order_time->slot_name << ":  " << order_time->slot_value;
  697. }
  698.  
  699. /*-----------------------------------------------location_class-------------*/
  700. location::location()
  701. {
  702.      address->slot_name = new char[SLOTNAME];
  703.      address->slot_name = "address"; 
  704.      address->slot_value = new char[SLOTVALUE];
  705.      *address->slot_value = NULL; 
  706.      phone->slot_name = new char[SLOTNAME];
  707.      phone->slot_name = "phone";
  708.      phone->slot_value = new char[SLOTVALUE];
  709.      *phone->slot_value = NULL;
  710.      invntory = new invent; 
  711. }
  712.  
  713. void location::set_slotval()
  714. {
  715.     cout << "\n" << address->slot_name << ":  ";
  716.     cin >> address->slot_value;è    cout << phone->slot_name << ":  ";
  717.     cin >> phone->slot_value;
  718.     invntory->set_slotval();
  719. }
  720.  
  721. Loc* location::get_slotval1()
  722. {
  723.     Loc* temp = new Loc;
  724.  
  725.     temp->address->slot_name = address->slot_name;
  726.     temp->address->slot_value = address->slot_value;
  727.     temp->phone->slot_name = phone->slot_name;
  728.     temp->phone->slot_value = phone->slot_value;
  729.     return temp;
  730. }
  731.  
  732. Inv* location::get_slotval2()
  733. {
  734.     Inv* temp = new Inv;
  735.  
  736.     temp = invntory->get_slotval();
  737.     return temp;
  738. }
  739.  
  740.  
  741. void location::prn_value()
  742. {
  743.     cout << "\n" << address->slot_name << ":  " << address->slot_value;
  744.     cout << "  " << phone->slot_name << ":  " << phone->slot_value;
  745.     invntory->prn_value();  /*print out the subclass inventory value*/
  746. }
  747.  
  748.  
  749. /*-------------------------------------------------------dealer_class-------*/
  750.  
  751.  
  752. void dealer::set_cust_slotval()
  753. {
  754.     custm->set_slotval();
  755. }
  756.  
  757. Cust* dealer::get_cust_slotval()
  758. {
  759.     Cust* temp = new Cust;
  760.  
  761.     temp = custm->get_slotval();
  762.     return temp;
  763. }
  764.  
  765. void dealer::prn_cust_slotval()
  766. {
  767.     custm->prn_value();
  768. }
  769.  
  770. /*-----------------------------------------------------warehs_class------*/è
  771. void warehs::set_sales_recs_slotval()
  772. {
  773.     sales_rec->set_slotval();    
  774. }
  775.  
  776. Sales_Rec* warehs::get_sales_recs_slotval()
  777. {
  778.     Sales_Rec* temp = new Sales_Rec;
  779.  
  780.     temp = sales_rec->get_slotval();
  781.     return temp;
  782. }
  783.  
  784.  
  785. /*--------------------------------------------sales_records class--*/
  786.  
  787. sales_records::sales_records()
  788. {
  789.      sale_number->slot_name = new char[SLOTNAME];
  790.      sale_number->slot_name = "sale_number";
  791.      sale_number->slot_value = 0;
  792.      number_sold->slot_name = new char[SLOTNAME];
  793.      number_sold->slot_name = "number_sold";
  794.      number_sold->slot_value = 0;
  795.      date->slot_name = new char[SLOTNAME];
  796.      date->slot_name = "date";    
  797.      date->slot_value = new char[SLOTVALUE];
  798.      *date->slot_value = NULL;
  799.      part->slot_name = new char[SLOTNAME];
  800.      part->slot_name = "part";
  801.      part->slot_value = new char[SLOTVALUE];
  802.      part->slot_value = NULL;
  803.      sold_to->slot_name = new char[SLOTNAME];
  804.      sold_to->slot_name = "sold_to";
  805.      sold_to->slot_value = new char[SLOTVALUE];
  806.      sold_to->slot_value = NULL;
  807.      cost->slot_name = new char[SLOTNAME];
  808.      cost->slot_name = "cost";
  809.      total_sale->slot_name = new char[SLOTVALUE];
  810.      total_sale->slot_name = "total_sale";
  811.      cost->slot_value = total_sale->slot_value = 0.0;
  812. }
  813.  
  814. void sales_records::set_slotval()
  815. {
  816.     cout << "\n" << sale_number->slot_name << ":  ";
  817.     cin >> sale_number->slot_value;
  818.     cout << date->slot_name << ":  ";
  819.     cin >> date->slot_value;
  820.     cout << part->slot_name << ":  ";
  821.     cin >> part->slot_value;
  822.     cout << number_sold->slot_name << ":  ";
  823.     cin >> number_sold->slot_value;
  824.     cout << cost->slot_name << ":  ";è    cin >> cost->slot_value;
  825.     cout << total_sale->slot_name << ":  ";
  826.     cin >> total_sale->slot_value;
  827.     cout << sold_to->slot_name << ":  ";
  828.     cin >> sold_to->slot_value;
  829. }
  830.  
  831. Sales_Rec* sales_records::get_slotval()
  832. {
  833.     Sales_Rec* temp = new Sales_Rec;
  834.  
  835.     temp->sale_number->slot_name = sale_number->slot_name;
  836.     temp->sale_number->slot_value = sale_number->slot_value;
  837.     temp->date->slot_name = date->slot_name;
  838.     temp->date->slot_value = date->slot_value;
  839.     temp->part->slot_name = part->slot_name;
  840.     temp->part->slot_value = part->slot_value;
  841.     temp->number_sold->slot_name = number_sold->slot_name;
  842.     temp->number_sold->slot_value = number_sold->slot_value;
  843.     temp->cost->slot_name = cost->slot_name;
  844.     temp->cost->slot_value = cost->slot_value;
  845.     temp->total_sale->slot_name = total_sale->slot_name;
  846.     temp->total_sale->slot_value = total_sale->slot_value;
  847.     temp->sold_to->slot_name = sold_to->slot_name;
  848.     temp->sold_to->slot_value = sold_to->slot_value;
  849.     return temp;
  850. }
  851.     
  852. void sales_records::prn_value()
  853. {
  854.     cout << "\n" << sale_number->slot_name << ":  " << sale_number->slot_value;
  855.     cout << "  " << date->slot_name << ":  " << date->slot_value;
  856.     cout << "  " << part->slot_name << ":  " << part->slot_value;
  857.     cout << "  " << number_sold->slot_name << ":  " << number_sold->slot_value;
  858.     cout << "  " << cost->slot_name << ":  " << cost->slot_value;
  859.     cout << "  " << total_sale->slot_name << ":  " << total_sale->slot_value;
  860.     cout << "  " << sold_to->slot_name << ":  " << sold_to->slot_value;
  861. }
  862.  
  863.  
  864.  
  865.  
  866. /*
  867. ***************************************************************
  868. **
  869. ** Building up slot name lists and their initialization
  870. **
  871. **  icskb1.cpp
  872. **  Define some funtions which  will be used to do initialization.
  873. **  Date:    9/6/88
  874. */
  875.  
  876. #include <stream.h>
  877. #include <string.h>
  878. #include <slotdef.h>è#include <tempstru.h>
  879. #include <icsclass.h>
  880. #include <ics.h>
  881.  
  882. extern stcust_class* STCUST_CLASS;      /*st_cust class node*/
  883. extern stinv_class*  STINV_CLASS;       /*st_inv class node*/
  884. extern sfinv_class*  SFINV_CLASS;       /*sf_inv class node*/
  885. extern warehs_class* WAREHS_CLASS;      /*warehs class node*/
  886. extern dealer_class* DEALER_CLASS;      /*dealer class node*/
  887. extern sfsalrec_class* SFSALREC_CLASS;  /*sf sale rec class node*/
  888.  
  889.  
  890. /*
  891. **  Because C++ does not support the dynamic defining the data structure.
  892. **  In order to do the reasoning in an object oriented knowledge base, the
  893. **  developer should initialize each class name, the slot names which defined
  894. **  in the class definition before doing the reasoning.
  895. */
  896.  
  897.  
  898. /*
  899. ** Make up the slot name list of the class
  900. ** Genaric function.
  901. */ 
  902.  
  903. cls_slot* make_up_cls_slot(char* clsname,char* slot[])
  904. {
  905.     cls_slot* temp = new cls_slot;
  906.     int i = 0;
  907.  
  908.     temp->name = clsname;
  909.     temp->sllist = NULL;
  910.     while(strcmp(slot[i],SLOT_END))
  911.     {
  912.         slot_node*  temp1 = new slot_node;
  913.         temp1->name = slot[i];
  914.         temp1->next = temp->sllist;
  915.         temp->sllist = temp1;
  916.         i++;
  917.     }
  918.     return temp;
  919. }
  920.  
  921.  
  922. /*
  923. **  Insert slot name list into the class node.
  924. **  Overloaded function.
  925. */
  926.  
  927. /*for sf_inv class*/
  928. void addslot_clsnd(cls_slot* c,stcust_class* s)
  929. {
  930.     /* The following are same for different class. Overloaded!!*/
  931.     s->name = c->name;
  932.     s->slist = c->sllist;è}
  933.  
  934. /*for sf_inv class*/
  935. void addslot_clsnd(cls_slot* c,sfinv_class* s)
  936. {
  937.     /*...... same as above ....*/
  938. }
  939.  
  940. /*for st_inv class*/
  941. void addslot_clsnd(cls_slot* c,stinv_class* s)
  942. {
  943.     /*...... same as above ....*/
  944. }
  945.  
  946. /*for warehs class*/
  947. void addslot_clsnd(cls_slot* c,warehs_class* s)
  948. {
  949.     /*...... same as above ....*/
  950. }
  951.  
  952. /*for dealer class*/
  953. void addslot_clsnd(cls_slot* c,dealer_class* s)
  954. {
  955.     /*...... same as above ....*/
  956. }
  957.  
  958. /*for sf sale rec class*/
  959. void addslot_clsnd(cls_slot* c,sfsalrec_class* s)
  960. {
  961.     /*...... same as above ....*/
  962. }
  963.  
  964.  
  965. /*--------------------Below is not generic function------------------------*/
  966. /*
  967. ** Initialize class node.
  968. */
  969.  
  970. void initstcustcls()        /*for st_cust class*/
  971. {
  972.     STCUST_CLASS = new stcust_class;
  973.  
  974. /*    *STCUST_CLASS->name = NULL;*/
  975.     STCUST_CLASS->slist = NULL;
  976.     STCUST_CLASS->elist = NULL;
  977. }
  978.  
  979. void initstinvcls()        /*for st_inv class*/
  980. {
  981.     STINV_CLASS = new stinv_class;
  982.  
  983. /*    *STINV_CLASS->name = NULL;*/
  984.     STINV_CLASS->slist = NULL;
  985.     STINV_CLASS->elist = NULL;
  986. }
  987.  
  988. void initsfinvcls()        /*for sf_inv class*/
  989. {
  990.     SFINV_CLASS = new sfinv_class;
  991.  
  992.     *SFINV_CLASS->name = NULL;
  993.     SFINV_CLASS->slist = NULL;
  994.     SFINV_CLASS->elist = NULL;
  995. }
  996.  
  997.  
  998. void initwarehscls()        /*for warehs class*/
  999. {
  1000.     WAREHS_CLASS = new warehs_class;
  1001.  
  1002.     *WAREHS_CLASS->name = NULL;
  1003.     WAREHS_CLASS->slist = NULL;
  1004.     WAREHS_CLASS->elist = NULL;
  1005. }
  1006.  
  1007. void initdealercls()        /*for dealer class*/
  1008. {
  1009.     DEALER_CLASS = new dealer_class;
  1010.  
  1011.     *DEALER_CLASS->name = NULL;
  1012.     DEALER_CLASS->slist = NULL;
  1013.     DEALER_CLASS->elist = NULL;
  1014. }
  1015.  
  1016. void initsfsalreccls()        /*for sf sale rec class*/
  1017. {
  1018.     SFSALREC_CLASS = new sfsalrec_class;
  1019.  
  1020.     *SFSALREC_CLASS->name = NULL;
  1021.     SFSALREC_CLASS->slist = NULL;
  1022.     SFSALREC_CLASS->elist = NULL;
  1023. }
  1024.  
  1025.  
  1026.  
  1027. /*************************************************************
  1028. **
  1029. **
  1030. ** Linking entity nodes into a tree structure for query
  1031. **
  1032. **  icskb2.cpp
  1033. **  Define some utility functions which are used to build up the KB.
  1034. **  Date:   9/6/88
  1035. */  
  1036.  
  1037.  
  1038. #include <stream.h>
  1039. #include <string.h>
  1040. #include <slotdef.h>
  1041. #include <tempstru.h>è#include <icsclass.h>
  1042. #include <ics.h>
  1043.  
  1044.  
  1045.  
  1046. /*------------------------------------------Class Node--------------------*/
  1047. extern stcust_class* STCUST_CLASS;  /*st_cust class node*/
  1048. extern stinv_class*  STINV_CLASS;   /*st_inv class node*/
  1049. extern sfinv_class*  SFINV_CLASS;   /*sf_inv class node*/
  1050. extern warehs_class* WAREHS_CLASS;  /*warehs class node*/
  1051. extern dealer_class* DEALER_CLASS;  /*dealer class node*/
  1052. extern sfsalrec_class* SFSALREC_CLASS;  /*sf sale rec class node*/
  1053.  
  1054. /*
  1055. **  Insert entity into the class node
  1056. **  Overloaded function.
  1057. */
  1058.  
  1059. /*For st cust class*/
  1060. void addety_clsnd(char* ename,st_cust* s,stcust_class* s1)
  1061. {
  1062.     stcust_etynd* temp = new stcust_etynd;
  1063.  
  1064.     /* The following are same for different class. Overloaded!!*/
  1065.     temp->name = ename;
  1066.     temp->entity = s;
  1067.     temp->next = s1->elist;
  1068.     s1->elist = temp;
  1069. }
  1070.  
  1071. /*For st inv class*/
  1072. void addety_clsnd(char* ename,st_inv* s,stinv_class* s1)
  1073. {
  1074.     stinv_etynd* temp = new stinv_etynd;
  1075.  
  1076.     /*...... same as above ....*/
  1077.  
  1078. }
  1079.  
  1080. /*For sf inv class*/
  1081. void addety_clsnd(char* ename,sf_inv* s,sfinv_class* s1)
  1082. {
  1083.     sfinv_etynd* temp = new sfinv_etynd;
  1084.  
  1085.     /*...... same as above ....*/
  1086.  
  1087. }
  1088.  
  1089. /*For warehs class*/
  1090. void addety_clsnd(char* ename,warehs* s,warehs_class* s1)
  1091. {
  1092.     warehs_etynd* temp = new warehs_etynd;
  1093.  
  1094.     /*...... same as above ....*/
  1095. }
  1096.  
  1097. /*For dealer class*/
  1098. void addety_clsnd(char* ename,dealer* s,dealer_class* s1)
  1099. {
  1100.     dealer_etynd* temp = new dealer_etynd;
  1101.  
  1102.     /*...... same as above ....*/
  1103. }
  1104.  
  1105. /*For dealer class*/
  1106. void addety_clsnd(char* ename,sf_sales_records* s,sfsalrec_class* s1)
  1107. {
  1108.     sfsalrec_etynd* temp = new sfsalrec_etynd;
  1109.  
  1110.     /*...... same as above ....*/
  1111. }
  1112.  
  1113. /*
  1114. **  Print slot name
  1115. **  Generic function.
  1116. */
  1117. void print_slot_name(slot_node* c)
  1118. {
  1119.     slot_node* temp;
  1120.  
  1121.     temp = c;
  1122.     cout << "\n" << "SLOT NAME :  ";
  1123.     while(temp != NULL)
  1124.     {
  1125.         cout <<  temp->name << "   ";
  1126.         temp = temp->next;
  1127.     }
  1128. }
  1129.  
  1130. /*
  1131. **  Print out entity.
  1132. **  Overloaded function
  1133. */
  1134.  
  1135. /*For st cust class*/
  1136. void print_entity_name(stcust_etynd* ety)
  1137. {
  1138.     stcust_etynd* temp;
  1139.     
  1140.     /* The following are same for different class. Overloaded!!*/
  1141.     temp = ety;
  1142.     cout << "\n" << "ENTITIES:  ";
  1143.     while(temp != NULL)
  1144.     {
  1145.         cout << "\n" << temp->name << ":";
  1146.         temp->entity->prn_value();
  1147.         temp = temp->next;
  1148.     }
  1149. }
  1150. è/*For st inv class*/
  1151. void print_entity_name(stinv_etynd* ety)
  1152. {
  1153.     stinv_etynd* temp;
  1154.  
  1155.     /*...... same as above ....*/
  1156. }
  1157.  
  1158. /*For sf inv class*/
  1159. void print_entity_name(sfinv_etynd* ety)
  1160. {
  1161.     sfinv_etynd* temp;
  1162.     
  1163.     /*...... same as above ....*/
  1164. }
  1165.  
  1166. /*For dealer class*/
  1167. void print_entity_name(dealer* ety)
  1168. {
  1169.     dealer* temp;
  1170.     
  1171.     /*...... same as above ....*/
  1172. }
  1173.  
  1174.  
  1175. /*
  1176. **  Print out class node.
  1177. **  Overloaded function
  1178. */
  1179.  
  1180. /*For st cust class*/
  1181. void print_clsnode(stcust_class* st)
  1182. {
  1183.     /* The following are same for different class. Overloaded!!*/
  1184.     cout << "\n" << "ClASS NAME:  " << st->name;
  1185.     print_slot_name(st->slist);
  1186.     print_entity_name(st->elist);
  1187. }
  1188.  
  1189. /*For st inv class*/
  1190. void print_clsnode(stinv_class* st)
  1191. {
  1192.     /*...... same as above ....*/
  1193. }
  1194.  
  1195. /*For sf inv class*/
  1196. void print_clsnode(sfinv_class* st)
  1197. {
  1198.     /*...... same as above ....*/
  1199. }
  1200.  
  1201. /*For warehs class*/
  1202. void print_clsnode(warehs* st)
  1203. {
  1204.     /*...... same as above ....*/}
  1205.  
  1206. /*For dealer class*/
  1207. void print_clsnode(dealer_class* st)
  1208. {
  1209.     /*...... same as above ....*/
  1210. }
  1211.  
  1212. /*For sf sale rec class*/
  1213. void print_clsnode(sfsalrec_class* st)
  1214. {
  1215.     /*...... same as above ....*/
  1216. }
  1217.  
  1218.  
  1219.  
  1220.  
  1221. Listing 7-3   Programs to Define Slots and Test Knowledge
  1222.                 Representation
  1223.  
  1224.  
  1225. /*
  1226. **   icsmain.cpp
  1227. **
  1228. **   This program is to implement the connction between Knowledge Base which
  1229. **   is made up of objects and the query about the KB. There are some steps 
  1230. **   required before doing such kind of reasoning.
  1231. **
  1232. **   The steps are:
  1233. **   1) define the class. e.g. structs.h.
  1234. **   2) define the kb. e.g. kbstruct.h
  1235. **   3) do the query.
  1236. ** 
  1237. **   The query's format is:
  1238. **         (entity ?x is-a ?y with slot_name = ?z)
  1239. **
  1240. **   Date:    8/5/88
  1241. **
  1242. */
  1243.  
  1244.  
  1245. #include <stream.h>
  1246. #include <string.h>
  1247. #include <slotdef.h>
  1248. #include <tempstru.h>
  1249. #include <icsclass.h>
  1250. #include <ics.h>
  1251. #include <parse.h>
  1252.  
  1253. #define MAXQUERY    60
  1254.  
  1255.  
  1256. /*
  1257. **  Because C++ does not support the dynamic defining the data structure.
  1258. **  In order to do the reasoning in an object oriented knowledge base, the
  1259. **  developer should initialize each class name, the slot names which defined
  1260. **  in the class definition before doing the reasoning.
  1261. */
  1262.  
  1263. /*------------------------------------------Class Node--------------------*/
  1264. stcust_class* STCUST_CLASS;      /*st_cust class node*/
  1265. stinv_class*  STINV_CLASS;       /*st_inv class node*/
  1266. sfinv_class*  SFINV_CLASS;       /*sf_inv class node*/
  1267. warehs_class* WAREHS_CLASS;      /*warehs class node*/
  1268. dealer_class* DEALER_CLASS;      /*dealer class node*/
  1269. sfsalrec_class* SFSALREC_CLASS;  /*sf sale rec class node*/
  1270.  
  1271.  
  1272. /* The initialization for each class' slot as follow.
  1273. ** the SLOT_END must be added in the end of each initialization.
  1274. */
  1275.  
  1276. /*for cust class*/
  1277. char* cust_slot[7] = { "dealer", "name", "part", "number", "order_date",
  1278.               "time", SLOT_END};
  1279.  
  1280. /*for inv class*/
  1281. char* inv_slot[4] = { "name", "cil", "oil", SLOT_END};
  1282.  
  1283. /*for warehs class*/
  1284. char* warehs_inv_slot[8] = { "name", "cil", "oil", "cost", "discount",
  1285.                     "min_order", "order_time", SLOT_END};
  1286. /*for location class*/
  1287. char* loc_slot[3] = { "address", "phone", SLOT_END};
  1288.  
  1289. /*for sales record class*/
  1290. char* salrec_slot[8] = { "sale_number", "date", "part", "number_sold", "cost",
  1291.                 "total_sale", "sold_to", SLOT_END};
  1292. /*-------------------------------------------------------------------------*/
  1293.  
  1294.  
  1295. main()
  1296. {
  1297.     cls_slot* temp1;
  1298.     extern void initstcustcls();
  1299.     extern void initstinvcls();
  1300.     extern void initsfinvcls();
  1301.     extern cls_slot* make_up_cls_slot(char*,char*[]);
  1302.  
  1303. /*For st_cust*/
  1304.     initstcustcls();
  1305.     char* temp = new char[40];
  1306.     temp = "st_cust";
  1307.         temp1 = make_up_cls_slot(temp, cust_slot);
  1308.     addslot_clsnd(temp1,STCUST_CLASS);
  1309.  
  1310. /*For entity creating and setting for st_cust*/ 
  1311.     st_cust* janoschka_stephen = new st_cust;
  1312.     char* te;
  1313.     te = "janoschka_stephen";
  1314.     addety_clsnd(te,janoschka_stephen,STCUST_CLASS);
  1315.  
  1316.     st_cust* lisa_hu = new st_cust;
  1317.     addety_clsnd("lisa_hu",lisa_hu,STCUST_CLASS);
  1318.  
  1319.  
  1320.     cout << "\n" << "Enter Janoschka Stephen's Data, Please";
  1321.     janoschka_stephen->set_slotval();
  1322.     cout << "\n" << "Enter Lise Hu's Data, Please";
  1323.     lisa_hu->set_slotval();
  1324.  
  1325.     print_clsnode(STCUST_CLASS);
  1326.  
  1327. /*----------------------------------------------------do some queries------*/
  1328.     char x[MAXQUERY];
  1329.     cout << "\nEnter query: ";
  1330.     gets(x);
  1331.     int i;
  1332.     extern int check_entity_pred_syntax(char*);
  1333.     i = check_entity_pred_syntax(x);
  1334.  
  1335.     extern char query_word[8][30];
  1336.     char* class_name;
  1337.     class_name = query_word[THREE];
  1338.  
  1339.     switch (i)
  1340.     {
  1341.               case CASE1:
  1342.         
  1343.         if(!strcmp(class_name,"st_cust"))
  1344.         {
  1345.             stcust_class* x;
  1346.             st_cust* y;
  1347.             x = STCUST_CLASS;
  1348.             y = find_entity_node(query_word[ONE],x);
  1349.             if ( y != NULL)
  1350.                 cout << "\nQuery is true!";
  1351.             else
  1352.                 cout << "\nQuery is not true!";
  1353.         }
  1354.         if(!strcmp(class_name,"st_inv"))
  1355.         {
  1356.             stinv_class* x;
  1357.             st_inv* y;
  1358.             x = STINV_CLASS;
  1359.             y = find_entity_node(query_word[ONE],x);
  1360.             y->prn_value();
  1361.         }
  1362.         if(!strcmp(class_name,"sf_inv"))
  1363.         {
  1364.             sfinv_class* x;
  1365.             sf_inv* y;
  1366.             x = SFINV_CLASS;
  1367.             y = find_entity_node(query_word[ONE],x);
  1368.             y->prn_value();
  1369.         }
  1370.         if(!strcmp(class_name,"warehs"))
  1371.         {
  1372.             warehs_class* x;
  1373.             warehs* y;
  1374.             x = WAREHS_CLASS;
  1375.             y = find_entity_node(query_word[ONE],x);
  1376.             y->prn_value();
  1377.         }
  1378.         if(!strcmp(class_name,"dealer"))
  1379.         {
  1380.             dealer_class* x;
  1381.             dealer* y;
  1382.             x = DEALER_CLASS;
  1383.             y = find_entity_node(query_word[ONE],x);
  1384.             y->prn_value();
  1385.         }
  1386.         if(!strcmp(class_name,"sfsalrec"))
  1387.         {
  1388.             sfsalrec_class* x;
  1389.             sf_sales_records* y;
  1390.             x = SFSALREC_CLASS;
  1391.             y = find_entity_node(query_word[ONE],x);
  1392.             y->prn_value();
  1393.         }
  1394.             break;
  1395.  
  1396.               case CASE2:
  1397.  
  1398.         if(!strcmp(class_name,"st_cust"))
  1399.         {
  1400.             stcust_class* x;
  1401.             stcust_etynd* y;
  1402.             x = STCUST_CLASS;
  1403.             y = find_all_entities(x);
  1404.             print_entity_name(y);
  1405.         }
  1406.         if(!strcmp(class_name,"st_inv"))
  1407.         {
  1408.             stinv_class* x;
  1409.             stinv_etynd* y;
  1410.             x = STINV_CLASS;
  1411.             y = find_all_entities(x);
  1412.             print_entity_name(y);
  1413.         }
  1414.         if(!strcmp(class_name,"sf_inv"))
  1415.         {
  1416.             sfinv_class* x;
  1417.             sfinv_etynd* y;
  1418.             x = SFINV_CLASS;
  1419.             y = find_all_entities(x);
  1420.             print_entity_name(y);
  1421.         }
  1422.         if(!strcmp(class_name,"warehs"))
  1423.         {
  1424.             warehs_class* x;
  1425.             warehs_etynd* y;
  1426.             x = WAREHS_CLASS;
  1427.             y = find_all_entities(x);
  1428.             print_entity_name(y);
  1429.         }
  1430.         if(!strcmp(class_name,"dealer"))
  1431.         {
  1432.             dealer_class* x;
  1433.             dealer_etynd* y;
  1434.             x = DEALER_CLASS;
  1435.             y = find_all_entities(x);
  1436.             print_entity_name(y);
  1437.         }
  1438.         if(!strcmp(class_name,"sfsalrec"))
  1439.         {
  1440.             sfsalrec_class* x;
  1441.             sfsalrec_etynd* y;
  1442.             x = SFSALREC_CLASS;
  1443.             y = find_all_entities(x);
  1444.             print_entity_name(y);
  1445.         }
  1446.             break;
  1447.               case CASE6:
  1448.  
  1449.         if(!strcmp(class_name,"st_cust"))
  1450.         {
  1451.             stcust_class* x;
  1452.             stcust_etynd* y;
  1453.             x = STCUST_CLASS;
  1454.             char* s1;
  1455.             char* s2;
  1456.             s1 = query_word[FIVE];
  1457.             s2 = query_word[SEVEN];
  1458.             y = find_e_with_slot(s1,s2,x);
  1459.             print_entity_name(y);
  1460.         }
  1461.         if(!strcmp(class_name,"st_inv"))
  1462.         {
  1463.             stinv_class* x;
  1464.             stinv_etynd* y;
  1465.             x = STINV_CLASS;
  1466.             char* s1;
  1467.             char* s2;
  1468.             s1 = query_word[FIVE];
  1469.             s2 = query_word[SEVEN];
  1470.             y = find_e_with_slot(s1,s2,x);
  1471.             print_entity_name(y);
  1472.         }
  1473.         if(!strcmp(class_name,"sf_inv"))
  1474.         {
  1475.             sfinv_class* x;
  1476.             sfinv_etynd* y;
  1477.             x = SFINV_CLASS;
  1478.             char* s1;
  1479.             char* s2;
  1480.             s1 = query_word[FIVE];
  1481.             s2 = query_word[SEVEN];
  1482.             y = find_e_with_slot(s1,s2,x);
  1483.             print_entity_name(y);
  1484.         }
  1485.         if(!strcmp(class_name,"warehs"))
  1486.         {
  1487.             warehs_class* x;
  1488.             warehs_etynd* y;
  1489.             x = WAREHS_CLASS;
  1490.             char* s1;
  1491.             char* s2;
  1492.             s1 = query_word[FIVE];
  1493.             s2 = query_word[SEVEN];
  1494.             y = find_e_with_slot(s1,s2,x);
  1495.             print_entity_name(y);
  1496.         }
  1497.         if(!strcmp(class_name,"dealer"))
  1498.         {
  1499.             dealer_class* x;
  1500.             dealer_etynd* y;
  1501.             x = DEALER_CLASS;
  1502.             char* s1;
  1503.             char* s2;
  1504.             s1 = query_word[FIVE];
  1505.             s2 = query_word[SEVEN];
  1506.             y = find_e_with_slot(s1,s2,x);
  1507.             print_entity_name(y);
  1508.         }
  1509.         if(!strcmp(class_name,"sfsalrec"))
  1510.         {
  1511.             sfsalrec_class* x;
  1512.             sfsalrec_etynd* y;
  1513.             x = SFSALREC_CLASS;
  1514.             char* s1;
  1515.             char* s2;
  1516.             s1 = query_word[FIVE];
  1517.             s2 = query_word[SEVEN];
  1518.             y = find_e_with_slot(s1,s2,x);
  1519.             print_entity_name(y);
  1520.         }
  1521.             break;
  1522.           }
  1523.  
  1524. }
  1525.  
  1526.  
  1527.  
  1528.  
  1529.  
  1530.  
  1531.         Listing 7-4  Sample Program for the Rule Structure
  1532.  
  1533. /*
  1534. ** Rule.h
  1535. */
  1536.  
  1537. typedef struct _rule {
  1538.   char *name;                /* the name of the rule */
  1539.   cons *premise;            /* the premise predicates */
  1540.   cons *conclusion;            /* the conclusion predicate */
  1541.   double certainty;            /* the certainty of this rule */
  1542.   struct _rule *next;            /* the next rule in the database */
  1543. } rule;
  1544.  
  1545. extern rule *make_rule();
  1546. extern rule *delete_rule();
  1547. extern rule *find_rule();
  1548. extern void add_rule();
  1549. extern void kill_rule();
  1550. extern void print_rule();
  1551. extern void printall_rules();
  1552.  
  1553. extern rule *RuleDatabase;            /* pointer to all rules */
  1554.  
  1555. /*
  1556. *******************************************************************
  1557.  
  1558. **
  1559. ** Rule.c
  1560. */
  1561. /*
  1562. **
  1563. ** (new format of rules)
  1564. */
  1565.  
  1566.  
  1567. #include <math.h>
  1568. #include <stdio.h>
  1569. #include "cons.h"
  1570. #include "rule.h"
  1571.  
  1572. rule *RuleDatabase = NULL;            /* pointer to all rules */
  1573.  
  1574. /*
  1575. ** read_rule(): reads the rule from the standard input. Rules look like 
  1576. ** this:
  1577. ** Rule-name IF (pred*) THEN pred
  1578. ** E.g:
  1579. ** Fuel-rule-1 IF ((fuel-level low)) THEN (out-of-fuel)
  1580. */
  1581.  
  1582. rule *read_rule()
  1583. {  rule *rp;
  1584.    cons *pp;
  1585.   
  1586.   if (rp = (rule *) malloc(sizeof (rule))) {
  1587.     pp = lread(C_FILE,stdin);            /* first the name */
  1588.     rp->name = pp->car.s;            /* save the name */
  1589.     free(pp);                    /* junk the cons */
  1590.     pp = lread(C_FILE,stdin);            /* get the 'IF' */
  1591.     if (strcmpi(pp->car.s,"IF")) {        /* not if? */
  1592.       free(rp);                    /* not a rule! */
  1593.       killcons(pp);
  1594.       return NULL;
  1595.     }
  1596.     killcons(pp);                /* get rid of it */
  1597.     rp->premise = lread(C_FILE,stdin);
  1598.     pp = lread(C_FILE,stdin);            /* get the 'THEN' */
  1599.     if (strcmpi(pp->car.s,"THEN")) {        /* not then? */
  1600.       killcons(rp->premise);            /* not a rule! */
  1601.       killcons(pp);
  1602.       free(rp);
  1603.       return NULL;
  1604.     }
  1605.     killcons(pp);                /* get rid of 'then' */
  1606.     rp->conclusion = lread(C_FILE,stdin);
  1607.     pp = lread(C_FILE,stdin);            /* lastly, the certainty */
  1608.     rp->certainty  = atof(pp->car.s);
  1609.     killcons(pp);                /* junk the certainty */
  1610.     rp->next = NULL;
  1611.   }
  1612.   return rp;
  1613. }
  1614.  
  1615. /*
  1616. ** _add_rule(): adds the rule _rp_ to the database _dbp_
  1617. */
  1618.  
  1619. static rule *_add_rule(rp,dbp)
  1620. rule *rp,*dbp;
  1621. {
  1622.   rule *rp2 = dbp;
  1623.   
  1624.   if (dbp == NULL) {
  1625.     return rp;
  1626.   }
  1627.   while (rp2->next) {
  1628.     rp2 = rp2->next;
  1629.   }
  1630.   rp2->next = rp;
  1631.   return dbp;
  1632. }
  1633.  
  1634. /*
  1635. ** add_rule(): adds the rule _rp_ to the RuleDatabase
  1636. */
  1637. void add_rule(rp)
  1638. rule *rp;
  1639. {
  1640.   RuleDatabase = _add_rule(rp,RuleDatabase);
  1641. }
  1642.  
  1643. rule *delete_rule(rp,dbp)
  1644. rule *rp,*dbp;
  1645. {
  1646.   rule *rrp;
  1647.   
  1648.   if (dbp == NULL) {
  1649.     return NULL;
  1650.   } else if (rp == dbp) {
  1651.     rrp = dbp->next;
  1652.     kill_rule(rp);
  1653.     return dbp->next;
  1654.   } else {
  1655.     dbp->next = delete_rule(rp,dbp->next);
  1656.     return dbp;
  1657.   }
  1658. }
  1659.  
  1660. /*
  1661. ** find_rule: find rule by name
  1662. */
  1663.  
  1664. rule *find_rule(s,dbp)
  1665. char *s;
  1666. rule *dbp;
  1667. {
  1668.   if (dbp == NULL) {
  1669.     return NULL;
  1670.   } else if (!strcmp(s,dbp->name)) {
  1671.     return(dbp);
  1672.   } else {
  1673.     return find_rule(s,dbp->next);
  1674.   }
  1675. }
  1676.      
  1677. void kill_rule(rp) 
  1678. rule *rp;
  1679. {
  1680.    killcons(rp->premise);
  1681.    killcons(rp->conclusion);
  1682.    free(rp);
  1683. }
  1684.  
  1685. void print_rule(rp)
  1686. rule *rp;
  1687. {
  1688.   printf("Name: %s\nPremise: ",rp->name);
  1689.   lprint(rp->premise,C_FILE,stdout);
  1690.   printf("\nConclusion: ");
  1691.   lprint(rp->conclusion,C_FILE,stdout);è  printf("\nCertainty: %g\n",rp->certainty);
  1692. }
  1693.  
  1694. void printall_rules(rule_base)
  1695. rule *rule_base;
  1696. {
  1697.   rule *db = rule_base;
  1698.   
  1699.   while (db != NULL) {
  1700.     print_rule(db);
  1701.     db = db->next;
  1702.   }
  1703. }
  1704.  
  1705.        Listing 7-5  Program for Logic Structure (Unstructured
  1706.                          Fact Base)
  1707.  
  1708. /*
  1709. ** fact.h
  1710. */
  1711. typedef struct _fact {
  1712.   struct _fact *next;
  1713.   cons         *predicate;
  1714.   double        cert;
  1715. } fact;
  1716.  
  1717.  
  1718. extern fact *PkbList;
  1719.  
  1720. /*
  1721. *******************************************************************
  1722. **
  1723. ** Facts.c
  1724. **
  1725. **
  1726. ** facts are stored as predicate-certainty pairs.
  1727. ** stash(): add a fact to the data base
  1728. ** 
  1729. */
  1730.  
  1731.  
  1732. /*-------------------------------------------------------include------------*/
  1733.  
  1734. #include <stdio.h>
  1735. #include "cons.h"
  1736. #include <math.h>
  1737. #include "fact.h"
  1738. #include "goal.h"
  1739.  
  1740.  
  1741. /*-------------------------------------------------------global variables---*/
  1742.  
  1743. fact *PkbList = NULL;
  1744.  
  1745.  
  1746. /*-------------------------------------------------------read_fact----------*/
  1747.  
  1748. fact *read_fact()
  1749. {
  1750.     fact *fp;
  1751.     cons *pp;
  1752.     
  1753.     if(fp = (fact *)malloc(sizeof(fact)))
  1754.     {
  1755.         fp->predicate = lread(C_FILE,stdin);
  1756.         pp = lread(C_FILE,stdin);
  1757.         fp->cert = atof(pp->car.s);
  1758.         killcons(pp);
  1759.         fp->next = NULL;è    }
  1760.     return fp;
  1761. }
  1762.  
  1763.  
  1764.  
  1765. /*-------------------------------------------------------_pkb_stash()-------*/
  1766.  
  1767. static fact *_pkb_stash(fp,fbp)
  1768. fact *fp,*fbp;
  1769. {
  1770.   fact *fp2 = fbp;
  1771.   
  1772.   if (fbp == NULL) {
  1773.     return fp;
  1774.   }
  1775.   while (fp2->next) {
  1776.     fp2 = fp2->next;
  1777.   }
  1778.   fp2->next = fp;
  1779.   return fbp;
  1780. }
  1781.  
  1782.  
  1783.  
  1784. /*-------------------------------------------------------pkb_stash()-------*/
  1785.  
  1786. void pkb_stash(predicate,cert)
  1787. cons *predicate;
  1788. double cert;
  1789. {
  1790.   fact *fp = (fact *) malloc(sizeof(fact));
  1791.   if (fp) {
  1792.     fp->predicate = predicate;
  1793.     fp->cert      = cert;
  1794.     fp->next      = NULL;
  1795.     PkbList = _pkb_stash(fp,PkbList);
  1796.   }
  1797. }
  1798.  
  1799.  
  1800.  
  1801. /*------------------------------------------------------print_fact()-------*/
  1802.  
  1803. void print_fact(fp)
  1804. fact *fp;
  1805. {
  1806.   printf("Predicate: "); lprint(fp->predicate,C_FILE,stdout);
  1807.   printf("\nCertainty: %lg\n\n",fp->cert);
  1808. }
  1809.  
  1810.  
  1811.  
  1812. /*------------------------------------------------------print_facts()-------*/
  1813. void printall_facts() {
  1814.   fact *fp = PkbList;
  1815.   
  1816.   while (fp != NULL) {
  1817.     print_fact(fp);
  1818.     fp = fp->next;
  1819.   }
  1820. }
  1821.  
  1822.  
  1823. /*-------------------------------------------------lookup_pkb_fact()-------*/
  1824.  
  1825. /*
  1826. **  lookup_pkb_fact(): Look up the pkb.
  1827. **  Return struct Ret_Pair (substitution,certainty).
  1828. **  Author:  Sony Y. Song
  1829. **  Date:    7/8/88
  1830. */
  1831.  
  1832.  
  1833. Ret_Pair *lookup_pkb_fact(pattern,prev_subst)
  1834. cons     *pattern;
  1835. cons     *prev_subst;
  1836. {
  1837.   Ret_Pair  *ret_pair;     
  1838.   fact *fp = PkbList;
  1839.   cons *subst;
  1840.   
  1841.   subst = NULL;
  1842.   if (ret_pair = (Ret_Pair *) malloc(sizeof(Ret_Pair)))
  1843.   {
  1844.     ret_pair->subst = NULL;
  1845.     ret_pair->certainty = 0.0;
  1846.   }
  1847.   else
  1848.   {
  1849.     puts("\n*** Yow! Out of core ***\n");
  1850.   }
  1851.   while (fp != NULL) {
  1852.     subst = unify_pred_c(pattern,fp->predicate);
  1853.     if (subst != NULL && !test_subst_used(subst,prev_subst))
  1854.     {
  1855.       ret_pair->subst = subst;
  1856.       ret_pair->certainty = fp->cert;
  1857.       break;
  1858.     } else {
  1859.       fp = fp->next;
  1860.     }
  1861.   }
  1862.   killcons(subst);
  1863.   return ret_pair;
  1864. }
  1865.  
  1866.  
  1867. /*-------------------------------------------------delete_PKB()-------*/
  1868. void delete_PKB()
  1869. {
  1870.     fact *PkbList = NULL;
  1871. }
  1872.  
  1873.